package com.flyduck.system.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.flyduck.system.entity.SysUserPostEntity;
import com.flyduck.system.mapper.SysUserPostMapper;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: flyduck-platform2
 * @description:
 * @author: flyduck
 * @create: 2021-04-23 14:51
 **/
@Repository
public class SysUserPostDao extends ServiceImpl<SysUserPostMapper, SysUserPostEntity> {

    /**
     * 根据用户id获取职位id集合
     * @param userId
     * @return
     */
    public List<Long> getPostIdsByUserId(Long userId) {
        List<SysUserPostEntity> sysUserPostEntityList = this.baseMapper.selectList(
                new QueryWrapper<SysUserPostEntity>().eq("user_id", userId)
        );
        return sysUserPostEntityList.stream().map(SysUserPostEntity::getPostId).collect(Collectors.toList());
    }

    /**
     * 新增用户职位关系集合
     * @param userId
     * @param postIds
     * @return
     */
    public int insertUserPostList(Long userId, List<Long> postIds) {
        int rows = 0;
        for (Long postId : postIds) {
            SysUserPostEntity sysUserPostEntity = new SysUserPostEntity();
            sysUserPostEntity.setUserId(userId);
            sysUserPostEntity.setPostId(postId);
            int row = this.baseMapper.insert(sysUserPostEntity);
            rows += row;
        }
        return rows;

    }

    /**
     * 根据用户id删除用户角色关系
     * @param userId
     * @return
     */
    public int deleteByUserId(Long userId) {
        return this.baseMapper.delete(new QueryWrapper<SysUserPostEntity>().eq("user_id", userId));
    }
}
